const { Sequelize, sequelize } = require('../../dao/db');
const Screen = require('../../models/Screen') //屏幕    
const User = require('../../models/User') // 用户
const Page = require('../../models/Page') // 页面
const Components = require('../../models/Component') // 组件
const ModulesDom = require('../../models/ModulesDom') // 配置

Screen.hasOne(Page, { foreignKey: 'id', sourceKey: 'defaultPage' })
Page.hasMany(Components, { foreignKey: 'pageId', sourceKey: 'id' })
Components.hasOne(ModulesDom, { foreignKey: 'moduleId', sourceKey: 'moduleId' })



const { nanoid } = require('nanoid')
const jwt = require('jsonwebtoken')
module.exports = (router) => {
    // 创建屏幕
    router.post('/createScreen', async (req, res) => {
        const { screenId, screenName, screenDesc, screenWidth, screenHeight, defaultPage, currentPage } = req.body

        const token = String(req.headers.authorization).split(' ').pop()
        const { userName } = jwt.verify(token, 'screen')
        const model = await User.findOne({ where: { userName } })


        await Screen.create({
            userId: model.dataValues.id,
            screenId: screenId || 'screen_' + nanoid(8),
            screenName,
            screenDesc,
            screenWidth,
            screenHeight,
            defaultPage,
            currentPage
        }).then(ress => {
            res.send({ code: 200, msg: '屏幕创建成功！！！' })
        }).catch((err) => {
            res.send({ code: 400, msg: err })
        });
    })
    // 创建页面
    router.post('/createPage', async (req, res) => {
        const { screenId, pageName, pageDesc } = req.body
        const token = String(req.headers.authorization).split(' ').pop()
        const { userName } = jwt.verify(token, 'screen')
        console.log(userName, '当前用户');
        const model = await User.findOne({ where: { userName } })

        // console.log(model.dataValues);
        const ScreenModel = await Screen.findOne({
            where: {
                screenId
            }
        })
        if (ScreenModel)
            return await Page.create({
                screenId,
                pageName,
                pageDesc
            }).then((result) => {
                res.send({ code: '200', msg: '页面创建成功 ' })
            }).catch((err) => {});
        res.send({ code: '400', msg: '当前屏幕不存在' })
    })
    // 添加组件
    router.post('/addComponents', async (req, res) => {
        const { screenId, pageId, moduleId, moduleName, componentName, attribute } = req.body
        const token = String(req.headers.authorization).split(' ').pop()
        const { userName } = jwt.verify(token, 'screen')
        console.log(userName, '当前用 户');
        const pageModel = await Page.findOne({ where: { screenId } })
        await Components.create({ pageId, moduleId, moduleName, componentName })
        const ModulesDomModel = await ModulesDom.create({ moduleId, ...attribute })

        if (ModulesDomModel)
            return res.send({ code: 200, msg: '组件添加成功！！！' })
        res.send({ code: 400, msg: '失败' })

    })
    // 查询用户屏幕列表
    router.get('/screenList', async (req, res) => {
        const token = String(req.headers.authorization).split(' ').pop()
        const { userName } = jwt.verify(token, 'screen')
        const model = await User.findOne({ where: { userName } })
        const screenlist = await Screen.findAll({
            where: {
                userId: model.dataValues.id
            }
        })
        res.send({ code: 200, data: screenlist })
    })
    // 查询布局文件
    router.get('/layout', async (req, res) => {
        const { screenId } = req.body


        const screen_page = await Screen.findAll({
            where: { screenId },
            include: {
                model: Page,
                include: {
                    model: Components,
                    include: {
                        model: ModulesDom,
                    },
                },
            }
        }).then((result) => {
            res.send(result)
        }).catch((err) => {
            res.send(err)
        });
    })
}